<?php

namespace app\components;

use Yii;
use yii\web\Controller;
use app\models\Tenant;
use yii\web\ServerErrorHttpException;
use app\components\Wechat;
use app\components\Loyalty;




class BaseWechateCardController extends Controller
{
    //初始化信息
    public function setConfig($appid,$openid)
    {
        // 删除session,重新设置appid
        $session = Yii::$app->session;
        $session->destroy();
        $session->set('appid', $appid);

        // 设置identifier
        $tenant = Tenant::find()->where(['appid' => $appid])->one();
        if (empty($tenant->identifier)) {
            throw new ServerErrorHttpException('identifier not found');
        }

        // 设置loyalty config
        if (empty($tenant->tenant_id) || empty($tenant->loyalty_accountid) || empty($tenant->loyalty_username) || empty($tenant->loyalty_password)) {
            throw new ServerErrorHttpException('loyalty config not found');
        } else {
            $loyaltyConfig = [
                'tenant_id' => $tenant->tenant_id,
                'app_id' => $tenant->appid,
                'loyalty_accountid' => $tenant->loyalty_accountid,
                'loyalty_username' => $tenant->loyalty_username,
                'loyalty_password' => $tenant->loyalty_password,
                'loyalty_url' => $tenant->loyalty_url,
            ];
            $session->set('loyaltyConfig', $loyaltyConfig);
        }
        //保存用户基础信息
        if (!empty($openid) && isset($openid)){
            $session->set('openid',$openid);
            $this->setUserInfo($openid);
        }
    }

    //设置用户信息
    public function setUserInfo($openid)
    {
        $userInfo = Loyalty::customer_get(['wechat_openid' => $openid]);
        if (isset($userInfo['code'])) {
            $this->setSession('user_info',$userInfo);
        }else{
            $this->setSession('user_info',"");
        }
    }

    //设置微信信息
    public function setWechatInfo($openid)
    {
        $wechatInfo = Wechat::userinfo([$openid]);
        if (!empty($wechatInfo)){
            $this->setSession('wechat_info',$wechatInfo);
        }else{
            $this->setSession('wechat_info',"");
        }
    }

    //设置session
    public function setSession($key,$value)
    {
        $session = Yii::$app->session;
        $session->set($key,$value);
    }
    //获取session
    public function getSession($key)
    {
        $session = Yii::$app->session;
        return $session->get($key);
    }

    //获取用户信息
    public function getUserInfo()
    {
        return $this->getSession("user_info");
    }

    //获取用户微信信息
    public function getWehcatInfo()
    {
        return $this->getSession("wechat_info");
    }

    //激活会员卡api
    public function ActivateMemberCard($code,$points)
    {
        $data = [
            "init_bonus" => $points,
            "init_bonus_record" => "旧积分同步",
            "membership_number" => "MOMO01",
            "code" => "$code",
        ];
        return Wechat::card_activate($data);
    }



    //报错页面
    public function error($message, $code = null)
    {
        $this->redirect(['errors/index', 'message' => $message, 'code' => $code]);
    }
}